package xio.leetcode.java;

/**
 * 633. Sum of Square Numbers (Easy)
 * <p>
 * 给定一个非负整数 c ，你要判断是否存在两个整数 a 和 b，使得 a^2 + b^2 = c。
 * <p>
 * 示例1:
 * <p>
 * 输入: 5
 * 输出: True
 * 解释: 1 * 1 + 2 * 2 = 5
 * <p>
 * 示例2:
 * <p>
 * 输入: 3
 * 输出: False
 * <p>
 * 思路：双指针遍历，一个从0开始+1，另一个从c的平方根整数值开始-1，注意这里这俩值是可以相等的。
 */
public class LC633_JudgeSquareSum {

    public boolean judgeSquareSum(int c) {
        int a = 0;
        int b = (int) Math.sqrt(c);
        while (a <= b) {
            int sum = a * a + b * b;
            if (sum == c) {
                return true;
            } else if (sum < c) {
                a++;
            } else {
                b--;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int c = 5;
        LC633_JudgeSquareSum sum = new LC633_JudgeSquareSum();
        System.out.println(sum.judgeSquareSum(c));
    }

}
